package com.linked.universal.utils;


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author Bluerain
 * @version 1.0
 * @data 2024/12/3 15:16
 * @describe todo
 */
@Slf4j
public class ZipUtils {


    public static void unzip(String zipFilePath, String destDirectory) {
        // 使用FileInputStream打开ZIP文件
        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry entry = zipIn.getNextEntry();
            // 使用while循环遍历ZIP内的条目
            while (entry != null) {
                log.info("-----文件名称：" + entry.getName());
                String filePath = destDirectory + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    // 如果是文件，则解压
                    extractFile(zipIn, filePath);
                } else {
                    // 如果是目录，则创建目录
                    new File(filePath).mkdirs();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
        } catch (IOException e) {

        }
    }


    public static void extractFile(InputStream inputStream, String filePath) {

        if (filePath.contains("/")) {
            new File(filePath.substring(0,filePath.lastIndexOf('/'))).mkdirs();
        }
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
            byte[] bytesIn = new byte[4096];
            int read = 0;
            while ((read = inputStream.read(bytesIn)) != -1) {
                bos.write(bytesIn, 0, read);
            }
        } catch (IOException e) {
            log.error("生成文件异常！", e);
        }
    }

    /**
     * @param sourceFolder 要打包的源文件目录或文件
     * @param destZipFile 打包后文件的目录
     * @throws IOException
     * */
    public static void compressFiles(String sourceFolder, String destZipFile) throws IOException {
        // 创建目标ZIP文件的FileOutputStream
        try (FileOutputStream fos = new FileOutputStream(destZipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {
            // 遍历要压缩的文件或文件夹
            File sourceFile = new File(sourceFolder);
            // 递归压缩文件
            compressFile(zos, sourceFile, "");
        }
    }

    private static void compressFile(ZipOutputStream zos, File file, String basePath) throws IOException {
        if (file.isDirectory()) {
            // 如果是目录，则添加目录到压缩包，并遍历目录中的文件
            File[] files = file.listFiles();
            if (files != null) {
                for (File subFile : files) {
                    // 递归压缩子文件或子目录
                    compressFile(zos, subFile, basePath + file.getName() + "/");
                }
            }
        } else {
            // 如果是文件，则添加文件到压缩包
            byte[] buf = new byte[1024];
            try (FileInputStream in = new FileInputStream(file)) {
                ZipEntry zipEntry = new ZipEntry(basePath + file.getName());
                zos.putNextEntry(zipEntry);

                int len;
                while ((len = in.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
            }
        }
    }
}
